﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations
@inject OrganizationApiService OrganizationApiService

<EditForm Model="OrganizationModel" OnValidSubmit="HandAddOrganization">

    <DataAnnotationsValidator />

    <div class="form-group">
        <InputText @bind-Value="OrganizationModel.Name" placeholder="Name" class="form-control"></InputText>
        <ValidationMessage For="()=>OrganizationModel.Name" class="text-danger"></ValidationMessage>
    </div>
    <div class="form-group">
        <InputText @bind-Value="OrganizationModel.Description" placeholder="Description" class="form-control"></InputText>
        <ValidationMessage For="()=>OrganizationModel.Description" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary mr-1">Add</button>
        <button @onclick="@(()=>ModalInstance.CancelAsync())" class="btn btn-warning text-dark mr-1">Cancel</button>
    </div>

</EditForm>

@code {

    #region Fields and Parameters



    /// <summary>
    /// Form Model
    /// </summary>
    AddOrganizationDto OrganizationModel = new AddOrganizationDto();



    /// <summary>
    /// Access to current modal
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance ModalInstance { get; set; }




    #endregion Fields and Parameters





    #region private Methods


    /// <summary>
    ///
    /// </summary>
    private async Task HandAddOrganization()
    {
        var result = await OrganizationApiService.AddAsync(OrganizationModel);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Organization_Added)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }



    #endregion private Methods

}